package main

import (
	"flag"
	"fmt"
	"os/exec"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

func Goshell(mod string) {
	var (
		cmd *exec.Cmd
		err error
	)
	cmd = exec.Command("sh", "-c", *Shellscript+" "+mod)
	err = cmd.Run()
	if err != nil {
		fmt.Println(err)
	}
	Count++
}

func Gorun(ch chan int64) {
	Goshell(*Module)
	//Count++
	ch <- 1
}

func Shellmain() {
	t1 := time.Now()
	flag.Parse()
	fmt.Println("Shell:", *Shellscript)
	maxs := *Thread
	fmt.Println("Threads:", maxs)
	chs := make([]chan int64, maxs)
	t2 := time.Now()
	d := t2.Sub(t1)
	for d < *Times {
		for i := 0; i < maxs; i++ {
			chs[i] = make(chan int64)
			go Gorun(chs[i])
		}
		for _, ch := range chs {
			<-ch
		}
		//     if *Module == "asyncpay" {
		//        go Goshell("sync")
		//     }
		t2 = time.Now()
		d = t2.Sub(t1)
	}
	if *Module == "asyncpay" {
		go Goshell("sync")
		fmt.Println(*Module)
	}
	t2 = time.Now()
	d = t2.Sub(t1)
	fmt.Println("Time Count:", d)
	fmt.Println("Count:", Count)
	fmt.Println("TPS:", Count/(int64(d)/1000000000))
}
